package edu.hawaii.wattwaiter.wattdepotcommands;

import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

/**
 * Tests the CarbonFlagSetter to ensure that
 * it is working properly.
 * @author David Mau
 */
public class TestCarbonFlagSetter {

  /**
   * Tests the normal operation of the CarbonFlagSetter.
   */
  @Test
  public void testCarbonFlagSetter() {
    // Create a new CarbonFlagSetter.
    CarbonFlagSetter test = new CarbonFlagSetter("2009-11-01");
    
    // Get the results from the class.
    List<Integer> results = test.getFlags(null);
    
    // Prepare known result.
    List<Integer> known = Arrays.asList( 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0,
        0, 0, 0, 0, 1, 1, 2, 2, 2 );
    
    assertEquals( "Checking that results and known data have same size Lists.", 
        results.size(), known.size() );
    
    // Compare the two lists.
    for ( int counter = 0; counter < 24; counter++ ) {
      assertEquals( "Checking items in the result list.", 
          results.get( counter), known.get( counter ));
    }
  }

  /**
   * Tests to see if the CarbonFlagSetter handles the situation correctly when it
   * receives a list of nothing but zeros.
   */
  @Test
  public void testAllZeroList() {
    // Create a new CarbonFlagSetter.
    CarbonFlagSetter test = new CarbonFlagSetter("Invalid Input Here");
    
    // Get the results from the class.
    List<Integer> results = test.getFlags(null);
    
    // Go through the list and ensure that everything is set to No Data.
    for ( Integer result : results ) {
      assertEquals( "Checking items in the error list.", 
          result.intValue(), CarbonFlagSetter.noDataFlag );
    }
  }
  
  /**
   * Tests to see if the flags would be distributed correctly if there are
   * some results missing.
   */
  @Test
  public void testHolesInData() {
    // Create a new CarbonFlagSetter.
    CarbonFlagSetter test = new CarbonFlagSetter("2009-11-01");
    
    // Prepare an input list with 4 holes.
    List<Double> fudgedInput = Arrays.asList( 0.0, 25.0, 20.0, 10.0, 5.0, 10.0, 29.0, 
        0.0, 30.0, 74.0, 75.0, 79.0, 88.0, 0.0, 90.0,
        95.0, 100.0, 95.0, 90.0, 60.0, 55.0, 25.0, 3.0, 0.0 );
    ArrayList<Double> directInput = new ArrayList<Double>();
    
    for ( Double single : fudgedInput ) {
      directInput.add( single );
    }
    
    // Place the input into the method.
    List<Integer> results = test.getFlags( directInput );
    
    // Prepare a list with known results.
    List<Integer> known = Arrays.asList( 3, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 3, 0,
        0, 0, 0, 0, 1, 2, 2, 2, 3 );
    
    assertEquals( "Checking that results and known data have same size Lists.", 
        results.size(), known.size() );
    
    // Test to make sure that the results match up with what is known.
    for ( int counter = 0; counter < 24; counter++ ) {
      assertEquals( "Checking items in the result list with holes.", 
          results.get( counter), known.get( counter ));
    }
  }
  
  /**
   * Tests to see if a call to getFlags with a list that doesn't have
   * 24 items is handled properly.
   */
  @Test
  public void testIncompleteListGiven() {
    // Create a new CarbonFlagSetter.
    CarbonFlagSetter test = new CarbonFlagSetter("2009-11-01");
    
    // Create a List of Doubles with only 1 item in it.
    List<Double> emptyList = Arrays.asList( 100.0 );
    
    // Get the results from the class.
    List<Integer> results = test.getFlags( emptyList );
    
    assertEquals( "Checking that the No Data List has 24 items.", 
        results.size(), 24 );
    
    // Go through the list and ensure that everything is set to No Data.
    for ( Integer result : results ) {
      assertEquals( "Checking items in the error list.", 
          result.intValue(), CarbonFlagSetter.noDataFlag );
    }
  }
  
}
